package com.um.jdy.platform.member.manager;

import com.um.jdy.common.finance.entity.po.PlatformBill;
import com.um.jdy.common.finance.entity.po.UserBill;
import com.um.jdy.common.finance.enums.FinancialEnum;
import com.um.jdy.common.finance.service.PlatformBillService;
import com.um.jdy.common.finance.service.UserBillService;
import com.um.jdy.common.member.entity.dto.WalletDTO;
import com.um.jdy.common.member.entity.po.Wallet;
import com.um.jdy.common.member.service.WalletService;
import com.um.jdy.platform.member.entity.vo.WalletItemVO;
import com.um.jdy.platform.member.entity.vo.WalletSummaryVO;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.orm.params.PageParams;
import com.um.springboot.starter.utils.OrderUtil;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Component
public class MemberWalletManager extends BaseManager<WalletService> {

    @Autowired
    UserBillService userBillService;

    @Autowired
    PlatformBillService platformBillService;

    public Response<Page<WalletItemVO>> page(PageParams pageParams) {
        Page<WalletItemVO> page = new Page<>();
        page.setPageNo(pageParams.getPageNo());
        page.setPageSize(pageParams.getPageSize());
        pageParams.getCondition().put("u.com_id",getComId());
        page = service.getCMSViewModelPageBySqlName(page, "get_member_wallet_page", "get_member_wallet_count",
                WalletItemVO.class, pageParams.getProperties(), pageParams.getValues(),
                pageParams.getRangeEntitys(), pageParams.getOrderNames(), pageParams.getSortOns());
        return Response.success(page);
    }

    public Response<Page<Wallet>> billPage(PageParams pageParams) {
        Page<Wallet> page = new Page<>();
        page.setPageNo(pageParams.getPageNo());
        page.setPageSize(pageParams.getPageSize());
        pageParams.getCondition().put("com_id",getComId());
        page = service.getPage(page, pageParams.getProperties(), pageParams.getValues(), pageParams.getRangeEntitys(), pageParams.getOrderNames(), pageParams.getSortOns());

        page.setExtraData(getWalletSummary(pageParams.getCondition().get("member_user_id"),getComId()));
        return Response.success(page);
    }

    /**
     * 钱包明细统计
     * @return
     */
    private WalletSummaryVO getWalletSummary(String userId, String comId) {
        WalletSummaryVO walletSummaryVO = service.getViewModel("get_member_wallet_summary",WalletSummaryVO.class,new Object[]{userId,comId,userId,comId,userId,comId});
        return walletSummaryVO;
    }

    /**
     * 充值添加用户收支流水，平台收支流水，如不适用，需要重写方法
     * @param dto
     * @return
     */
    @Transactional
    public Response<Wallet> addEntity(WalletDTO dto){
        String orderNo = OrderUtil.getOrderNo();

        Wallet entity = UMBeanUtils.copyBean(dto, Wallet.class);

        entity.setMemberWalletId(service.createUUIDPrimaryId());
        entity.setNo(orderNo);
        entity.setCreateTime(new Date());
        entity.setEvent(FinancialEnum.WalletBillEvent.Artificial.name());
        entity.setStatus(FinancialEnum.BillStatus.Effective.name());
        service.add(entity);

        //插入用户收银流水
        UserBill userBill = new UserBill();
        userBill.setMemberUserId(entity.getMemberUserId());
        userBill.setNo(OrderUtil.getOrderNo());
        userBill.setOutTradeNo("");
        userBill.setOrderNo(orderNo);
        userBill.setAmount(entity.getAmount());
        userBill.setType(entity.getType());//收入支出类型：0-收入，1-支出
        userBill.setEvent(FinancialEnum.UserBillEvent.Artificial.name());
        userBill.setStatus(FinancialEnum.BillStatus.Effective.name());//账单状态：0未生效|1已生效|2已失效
        userBill.setCreateTime(new Date());
        userBill.setRemark(entity.getRemark());
        userBillService.save(userBill);

        //插入平台收银流水
        PlatformBill platformBill = new PlatformBill();
        platformBill.setNo(OrderUtil.getOrderNo());
        platformBill.setOutTradeNo("");
        platformBill.setOrderNo(orderNo);
        platformBill.setAmount(entity.getAmount());
        //人工用户充值，收支类型应与用户一致
        platformBill.setType(entity.getType());//收入支出类型：0-收入，1-支出
        platformBill.setEvent(FinancialEnum.PlatformBillEvent.Artificial.name());
        platformBill.setStatus(FinancialEnum.BillStatus.Effective.name());//账单状态：0未生效|1已生效|2已失效
        platformBill.setCreateTime(new Date());
        platformBill.setRemark(entity.getRemark());
        platformBillService.save(platformBill);

        return Response.success(entity);
    }
}
